const CryptoJS = require("crypto-js");
const axios = require(`axios`)
const config = {
    // token 信息
    tokens: [
        {
            token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyZDBiNDMwZDgyMjY0MmRlYjc3NjQzMjE0MTdmODZhYS1mYjM0OTEwZjQ3NzJmYTdiMzRlMWM0MjU3ZTZiNTQ4NiIsImlhdCI6MTY5NTU2NTgxNiwiZXhwIjoxNzAzMzQxODE2fQ.NTp9b1RjPKpeq6jumYvl4sPw7Iec5p387LonPYPX337c7gl3cLO5s9tu7AAXLjxko6x4ecYBaZckgTqQHubrRQ',
            name: '白羊152'
        },
        // {
        //     token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyZDBiNDMwZDgyMjY0MmRlYjc3NjQzMjE0MTdmODZhYS0wNTI4NDRlNDA0MTFmY2I4OWMyODUxNDAyMzllM2QwYiIsImlhdCI6MTY5NTU3MDY0MSwiZXhwIjoxNzAzMzQ2NjQxfQ.xFQkW8bR9dlsD3qO6HEgnKUtztq9laXkUfFQtdo2uQmILKJPz-mw5KEVZtItYZH9_VRlfg84sjfHmiYz2F6K-g',
        //     name: '大鑫173'
        // }
    ],
    // 抢购延迟
    delayTime: 1500,
    // 网络繁忙 休息延迟
    restTime: 1000 * 40
}
function main() {
    // 每 10 次 封 40 秒 IP
    let i = 1
    let tokenIndex = 0
    new Promise(async (resolve) => {
        for (;;) {
            try {
                const token = config.tokens[tokenIndex]
                const resp = await buy(token.token)
                console.log(`[${token.name}] [${i++}]次 结果`, resp.data)
                if(resp.data.msg === '请求过于频繁,请稍候再试!') {
                    console.log(`请求过于频繁, 建议调大 delayTime(抢购延迟) 当前延迟 ${config.delayTime} 毫秒`)
                }
                if(resp.data.msg === '网络繁忙，请稍后再试。') {
                    console.log(`网络繁忙休息 ${config.restTime / 1000} 秒...`)
                    await sleep(config.restTime)
                    tokenIndex += 1;
                }
                if(tokenIndex == config.tokens.length) {
                    tokenIndex = 0
                }
                await sleep(config.delayTime)
            } catch (error) {
                if(error.message === 'Client network socket disconnected before secure TLS connection was established') {
                    console.log('error', '网络不好，响应超时')
                } else {
                    console.log('error', error)
                    await sleep(config.delayTime)
                }
            }
        }
    })
}

function sleep(delay) {
    return new Promise((resolve) => {
        setTimeout(resolve, delay)
    })
}


function getSign(now) {
    // 数据
    const data = `73b34182aaed4559b56e5504801f557b|4d3fdf97a5424568ae421d876d4cd5e5|4bd4ba737214495db86869b1d40e9552|ZH202012300001|T0002|${now}`;
    // 密钥
    const key = '6e4d4152306744616873434157757a5a46476c70727654304a4b326a68453549';
    const pKey = CryptoJS.enc.Hex.parse(key)
    const encrypted = CryptoJS.AES.encrypt(data, pKey, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
    });
    return encrypted.ciphertext.toString()
}

function buy(token) {
    const now = new Date().getTime()
    const sign = getSign(now)
    return axios.post(`https://vtravel.link2shops.com/vfuliApi/api/client/ypJyOrder/submitWithSign`, {
        "pageTag": "szcr",
        "openId": null,
        "activityId": "4d3fdf97a5424568ae421d876d4cd5e5",
        "goodsId": "4bd4ba737214495db86869b1d40e9552",
        "channelId": "73b34182aaed4559b56e5504801f557b",
        "rechargeAccount": "",
        "rechargeType": "1",
        "notifyMobile": "18336421281",
        "merchantCode": "ZH202012300001",
        "platformTp": "T0002",
        "bankType": "CCB",
        "cardBin": "489592",
        "version": "V2.0.0",
        "sourceTp": "H5",
        "timestamp": now,
        "memberName": "",
        "memberAccount": "",
        "sign": sign,
        "tertiarySource": "02",
        "homeUrl": "https://vtravel.link2shops.com/yiyuan/#/",
        "channelDistinct": ""
    }, {
        headers: {
            Accept: "application/json, text/plain, */*",
            "Content-Type": "application/json",
            Host: "vtravel.link2shops.com",
            Origin: "https://vtravel.link2shops.com",
            Referer: "https://vtravel.link2shops.com/yiyuan/?uuid=B6uu8DDpWIrA2RmtWjFrxgWhIezW+GuoHrlF/P2FV0g=",
            "User-Agent": "Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Mobile Safari/537.36 Edg/117.0.0.0",
            token: token
        }
    })
}


main()